package com.flowerfling;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.MaxReward;
import com.applovin.mediation.MaxRewardedAdListener;
import com.applovin.mediation.ads.MaxRewardedAd;

import org.json.JSONObject;


public class FlingMaxReward {
    private static FlingMaxReward adManager;
    private MaxRewardedAd maxRewardedAd;
    private String adSource="";

    public static FlingMaxReward getInstance() {
        if (adManager == null) {
            adManager = new FlingMaxReward();
        }
        return adManager;
    }

    public void loadRewardAd(Context context) {
        getRewardAD(context);
        maxRewardedAd.loadAd();
    }


    public boolean isReady(){
        if (maxRewardedAd != null) {
            return maxRewardedAd.isReady();
        }
        return false;
    }

    public void showAd(Activity activity, String adSource) {
        if (!isReady()) {
            callbackAdError();
            maxRewardedAd.loadAd();
            return;
        }
        if (!TextUtils.isEmpty(adSource)){
            this.adSource = adSource;
        }
        getRewardAD(activity);
        maxRewardedAd.showAd();
    }

    private void getRewardAD(Context context) {
        if (maxRewardedAd == null) {
            maxRewardedAd =MaxRewardedAd.getInstance(FlingConstant.REWARD_PLACEMENTID, context);
            maxRewardedAd.setListener(new AdListener());
        }
    }

    public class AdListener implements MaxRewardedAdListener{

        @Override
        public void onUserRewarded(@NonNull MaxAd maxAd, @NonNull MaxReward maxReward) {

        }

        @Override
        public void onAdLoaded(@NonNull MaxAd maxAd) {

        }

        @Override
        public void onAdDisplayed(@NonNull MaxAd maxAd) {
        }

        @Override
        public void onAdHidden(@NonNull MaxAd maxAd) {
            maxRewardedAd.loadAd();
            callbackAdOver(adSource);
        }

        @Override
        public void onAdClicked(@NonNull MaxAd maxAd) {

        }

        @Override
        public void onAdLoadFailed(@NonNull String s, @NonNull MaxError error) {
        }

        @Override
        public void onAdDisplayFailed(@NonNull MaxAd maxAd, @NonNull MaxError error) {
            callbackAdError();
        }
    }

    public void callbackAdError() {
        try {
            FlingCocosBridge.getInstance().ADVideoCallback("ad_error_filing", "{}");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void callbackAdOver(String adSource) {
        try {
            JSONObject jsonObject = getJson(adSource);
            FlingCocosBridge.getInstance().ADVideoCallback("ad_over_filing", jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private JSONObject getJson(String adSource) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("ad_source", adSource);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return jsonObject;
    }

}
